#include"Slist.h"

Slist* BuyNode(typedata x)
{
	Slist* newnode = (Slist*)malloc(sizeof(Slist));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void Print(Slist* phead)
{
	Slist* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
void FrontPush(Slist** phead,typedata x)
{
	assert(phead);
	Slist* first = *phead;
	Slist* newnode = BuyNode(x);
	
	*phead = newnode;
	newnode->next = first;
}
void BackPush(Slist** phead,typedata x)
{
	assert(phead);

	Slist* cur = *phead;
	Slist* newnode = BuyNode(x);
	while (cur->next)
	{
		cur = cur->next;

	}
	cur->next = newnode;

}
void FrontPop(Slist** phead)
{
	assert(phead);

	assert(*phead);

	Slist* first = (*phead)->next;
	free(*phead);
	*phead = first;

}

void BackPop(Slist** phead)
{
	assert(phead);

	assert(*phead);

	Slist* cur = *phead;
	while (cur->next->next)
	{
		cur = cur->next;
	}
	free(cur->next);
	cur->next = NULL;
}

Slist*  SlistFind(Slist* phead, typedata x)
{
	assert(phead);
	Slist* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SLinsert(Slist** phead, Slist* pos, typedata x)
{
	assert(*phead);
	assert(phead);
	Slist* cur = *phead;
	Slist* first = BuyNode(x);
	while (cur->next != pos)
	{
		cur = cur->next;

	}
	first->next = pos;
	cur->next = first;
}
void SLinsertAfter(Slist* pos, typedata x)
{
	assert(pos);
	Slist* next = pos->next;
	Slist* newnode = BuyNode(x);
	pos->next = newnode;
	newnode->next = next;
}

void SLErase(Slist** phead, Slist* pos)
{
	assert(*phead);
	assert(pos);
	Slist* cur = *phead;
	Slist* next = pos->next;
	while (cur->next!= pos)
	{
		cur = cur->next;
	}
	free(pos);
	cur->next = next;
}

void SLEraseAfter(Slist* pos)
{
	assert(pos);
	Slist* next = pos->next->next;
	free(pos->next);
	pos->next = next;
}